// pages/addressmanage/addressmanage.js
var GlobalData = require('../../utils/GlobalData.js');
var dataManager = require('../../utils/dataManager.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowEditor: false,
    addressArr: [],
    windowHeight: 1106,
    // 操作类型
    opType: 0,
    url: "",
    isclickEdit: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.url) {
      this.data.url = options.url
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.windowHeight) {
      var height = app.globalData.windowHeight;
      this.data.windowHeight = height - height * 0.0487;
      this.setData({
        windowHeight: this.data.windowHeight
      })
    }
    /**获取默认的收货地址 */
    if (GlobalData.addressArr.length)
      // this.addressArr = GlobalData.addressArr;
      this.onAddressComplete();
    else {
      this.getShoppingAddress();
    }
  },
  getShoppingAddress() {
    dataManager.rigisterObserver("getshippingaddress", this.onAddressComplete, this);
    dataManager.getAddressData()
  },
  /**获取收货地址成功的回调函数 */
  onAddressComplete() {
    this.data.isclickEdit = true;
    if (this.data.opType == 3) {
      app.onshowModal("删除成功")
      this.data.opType = 0;
    }
    this.data.addressArr = GlobalData.addressArr;

    if (this.data.addressArr.length) {
      for (let i = 0; i < this.data.addressArr.length; i++) {
        if (this.data.addressArr[i].isdefault) {
          this.data.addressArr[i]["defaultTxt"] = "默认地址";
        } else
          this.data.addressArr[i]["defaultTxt"] = "设为默认";
      }
    }
    this.setData({
      addressArr: this.data.addressArr
    })
    if (!GlobalData.addressArr.length)
      GlobalData.shoppingAddress = null;
  },
  /**1:设为默认地址 2:编辑地址 3:删除*/
  onEditAddress(e) {
    // data, type
    let data = e.currentTarget.dataset.item;
    let type = parseInt(e.currentTarget.dataset.id);
    if (this.data.isclickEdit) {

      switch (type) {
        case 1:
          if (data.isdefault == 1) {
            data.isdefault = 0;
          } else {
            data.isdefault = 1;
          }
          this.data.isclickEdit = false;
          this.onExchangeData(data);
          break;
        case 2:
          GlobalData.addressTitle = data.id;
          GlobalData.editInfo = data;
          dataManager.UseNavigateTo("/pages/appendaddress/appendaddress", false);
          // this.router.navigateByUrl("appendaddress/addressmanage" + GlobalData._random());
          break;
        case 3:
          this.data.isclickEdit = false;
          this.onDelAddress(data.id);
          break;
      }
    }

  },
  /**更新收货地址 */
  onExchangeData(data) {
    // let obj = {};
    // obj["id"] = data.id;
    // obj["name"] = data.name;
    // obj["phone"] = data.phone;
    // obj["address"] = data.address;
    // obj["isdefault"] = data.isdefault;


    dataManager.rigisterObserver("updateshippingaddress", this.onComplete, this);
    dataManager.onEditAddressDm(data);
  },
  /**删除地址 deleteshippingaddress*/
  onDelAddress(id) {
    this.data.opType = 3
    dataManager.rigisterObserver("deleteshippingaddress", this.onComplete, this);
    dataManager.onDelAddressDm(id);
  },

  /**获取收货地址 */
  onComplete() {
    dataManager.rigisterObserver("getshippingaddress", this.onAddressComplete, this);
    dataManager.getAddressData()
    // this.getShoppingAddress()
  },
  /**添加收货地址 */
  onAddAddress() {
    GlobalData.addressTitle = 0
    dataManager.UseNavigateTo("/pages/appendaddress/appendaddress", false);

    // this.router.navigateByUrl("appendaddress/addressmanage" + GlobalData._random())
  },
  /**选择收货地址 */
  onSelShoppingAddress(e) {
    let item = e.currentTarget.dataset.item;
    if (this.data.url) {
      if (this.data.url == "integralactivities" || this.data.url == "integrallotteryrecord")
        GlobalData.prixzeAddress = item;
      else
        GlobalData.shoppingAddress = item;
      // GlobalData.prixzeAddress = item;  抽奖记录
      wx.navigateBack({
        delta: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})